<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CIBlog
 *
 * 使用codeigniter框架开发的一个博客。
 * 
 * @package     ciblog
 * @author      Eric
 * @copyright   Copyright (c) 2011 - 2012, Eric
 * @license     GNU General Public License 2.0
 * @link        http://www.hebaijun.com
 * @version     1.0
 */

 //----------------------------------------------------------

 /**
  * 取系统设置的模型
  *
  * 
  *
  * @package     ciblog
  * @subpackage  Models
  * @category    Models
  * @author      Eric <baijun.he@163.com>
  * @link        http://www.hebaijun.com
  */
class Option_m extends CI_Model {

	/**
     * 构造函数
     * 
     * @access public
     * @return void
     */
    public function __construct()
    {
		parent::__construct();
		
		log_message('debug', "CIBlog: Option Model Class Initialized");

		//加载缓存适配器，如果memcached不支持 则用file类型缓存
		$this->load->driver('cache', array('adapter' => config_item('cache_adapter'), 'backup' => config_item('cache_backup')));
		//加载数据库连接,已经改成自动连接
		//$this->load->database();
    }

	/**
     * 通过名称取得系统的一些设置
     * 
     * @access public
	 * @param string $option 名称
     * @return mixed 取决于数据库保存的内容
     */
    public function get_option( $option, $default = FALSE )
    {
		//返回值
		$value = '';

		$option = trim($option);
		if(empty($option))
		{
			return FALSE;
		}

		$not_option = $this->cache->get('not_option_'.$option);
		if( $not_option )
			return $default;

		$all_options = $this->load_all_options();
		if(isset($all_options[$option]))
		{
			$value = $all_options[$option];
		}
		else
		{
			$value = $this->cache->get('option_'.$option);
			if($value === FALSE)
			{
				$table_option = $this->db->dbprefix('options');
				$query = $this->db->query("SELECT option_value FROM $table_option WHERE option_name = ? LIMIT 1",$option);
				if ($query->num_rows() > 0)
				{
				   $row = $query->row(); 
				   $value = $row->option_value;
				   $this->cache->save('option_'.$option,$value,config_item('cache_expire'));
				}
				else
				{
					$this->cache->save('not_option_'.$option,1,config_item('cache_expire'));
					return $default;
				}
			}
		}

		if ( in_array( $option, array('siteurl', 'home') ) )
			$value = rtrim($value, '/');

		return $value;
    }

	/**
     * 取得所有的设置
     * 
     * @access public
     * @return array 所有自动加载的系统设置
     */
    public function load_all_options()
    {
		//返回值
		$all_options = FALSE;

		//$this->cache->delete('all_options');
		$all_options = $this->cache->get('all_options');

		if($all_options === FALSE)
		{
			$table_option = $this->db->dbprefix('options');
			$query = $this->db->query("SELECT option_name, option_value FROM $table_option WHERE autoload = 'yes'");
			if ($query->num_rows() > 0)
			{
				$all_options = array();
				foreach ($query->result() as $row)
				{
					$all_options[$row->option_name] = $row->option_value;
				}
				$this->cache->save('all_options',$all_options,config_item('cache_expire'));
			}
		}

		return $all_options;
    }	
}

/* End of file option.php */
/* Location: ./application/models/option.php */